package com.example.androidtvdemo.demo05;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.leanback.widget.Presenter;

import com.bumptech.glide.Glide;
import com.example.androidtvdemo.R;
import com.example.androidtvdemo.net.BlogSubArticle;

public class ArticleTestPresenter extends Presenter {

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_blog_article_v2, parent, false);
        return new Holder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        Holder holder = (Holder) viewHolder;
        if (item instanceof BlogSubArticle.DataBean.ContentBean) {
            Log.i("->>", "onBindViewHolder: 加载数据");
            //holder.mTextView.setText((String) item);
            BlogSubArticle.DataBean.ContentBean data = (BlogSubArticle.DataBean.ContentBean) item;
            Glide.with(((Holder) viewHolder).mImageView.getContext())
                    .load("http://8.129.19.172:2020/portal/image/" + data.getCover())
                    .into(holder.mImageView);
            holder.mTitle.setText(data.getTitle());
        } else {
            Log.i("->>", "onBindViewHolder: 数据不对");
        }
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {

    }

    public static class Holder extends ViewHolder {
        private ImageView mImageView;
        private TextView mTitle;

        public Holder(View view) {
            super(view);
            mImageView = view.findViewById(R.id.iv_article_cover);
            mTitle = view.findViewById(R.id.tv_title);
        }
    }
}
